package com.liang.demo.controller;

import com.liang.demo.base.BaseApiController;
import com.liang.demo.base.RestResponse;
import com.liang.demo.po.Admin;
import com.liang.demo.service.AdminService;
import com.liang.demo.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/19 15:42
 */
@RestController
public class LoginController extends BaseApiController {

    @Autowired
    private AdminService adminService;


    @PostMapping("/api/login")
    public RestResponse<String> login(@RequestBody Map<String, String> params,
                                      HttpServletResponse rsp) {
        //不做密码加密了
        String userName = params.get("userName");
        String password = params.get("password");
        Admin admin = adminService.login(userName, password);
        if (admin == null) {
            return RestResponse.fail(401, "用户名或密码错误!");
        } else {
            String token = JwtUtil.createToken(admin.getId().toString());
            return RestResponse.ok(token);
        }
    }

    @GetMapping("/api/logout")
    public RestResponse<String> logout(String token) {
        return RestResponse.ok();
    }
}
